package com.example.system.controller.question;

import com.example.common.core.domain.R;
import com.example.common.core.domain.ResultCode;
import com.example.common.core.domain.TableDataInfo;
import com.example.system.Service.question.IQuestionService;
import com.example.system.domain.question.dto.QuestionAddDTO;
import com.example.system.domain.question.dto.QuestionEditDTO;
import com.example.system.domain.question.dto.QuestionQueryDTO;
import com.example.system.domain.question.vo.QuestionDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {

    @Autowired
    private IQuestionService questionService;

    @GetMapping("/list")
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        log.info("/question/list -> QuestionQueryDTO:{}", questionQueryDTO.toString());
        return questionService.list(questionQueryDTO);
    }

    @PostMapping("/add")
    public R<Void> add(@RequestBody QuestionAddDTO questionAddDTO) {
        log.info("/question/add -> QuestionAddDTO:{}", questionAddDTO.toString());
        int ret = questionService.add(questionAddDTO);
        if (ret == 1) {
            return R.ok(ResultCode.SUCCES_ADD_QUESTION);
        } else if (ret == 0) {
            return R.fail(ResultCode.FAILED_ADD_QUESTION);
        }
        return R.fail();
    }

    @GetMapping("/detail")
    public R<QuestionDetailVo> getQuestionDeatilById(Long questionId) {
        log.info("/question/detail -> questionId:{}", questionId);
        QuestionDetailVo question = questionService.getQuestionDetailById(questionId);
        return R.ok(question);
    }

    @PostMapping("/edit")
    public R<Void> editQuestionById(@RequestBody QuestionEditDTO questionEditDTO) {
        log.info("/question/edit -> QuestionEditDTO:{}", questionEditDTO);
        return questionService.editQuestionById(questionEditDTO);
    }

    @DeleteMapping("/delete")
    public R<Void> deleteQuestionById(Long questionId) {
        log.info("/question/delete -> questionId:{}", questionId);
        return questionService.deleteQuestionById(questionId);
    }
}
